//
//  WKOneThroughTableViewController.m
//  WKDemo
//
//  Created by wangzhaohui-Mac on 14-8-2.
//  Copyright (c) 2014年 com.app. All rights reserved.
//

#import "WKTwoThroughViewController.h"
#import "WKOnlyTextHttpTool.h"
#import "WKOnlyTextParam.h"
#import "WKOnlyTextResult.h"
#import "WKTableViewCell.h"
#import "WKTableViewCellFrame.h"
#import "WKCellPuthinfoTableView.h"

@interface WKTwoThroughViewController ()
/**模型数组*/
@property (nonatomic,strong) NSMutableArray *statuses;
@property (nonatomic , strong)WKTableViewCellFrame *tablecell;
@end

@implementation WKTwoThroughViewController

- (NSMutableArray *)statuses{
    if (!_statuses) {
        _statuses = [NSMutableArray array];
    }
    return _statuses;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    

}

- (void)loacldNew
{
    WKOnlyTextParam *param = [[WKOnlyTextParam alloc] init];
    [WKOnlyTextHttpTool getThrougNewDataListWithParam:param success:^(WKOnlyTextResult *result) {
        //如果返回数据为空则返回;
        if (result == nil) return;
        //        //[self.statuses addObjectsFromArray:result.homeDatas];
        //[self.statuses addObjectsFromArray:result.homeDatas];
        NSRange range = NSMakeRange(0, result.homeDatas.count);
        NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
        [self.statuses insertObjects:result.homeDatas atIndexes:set];
     
        //刷新数据
        [self.tableView reloadData];
        //显示底部刷新控件
        self.tableView.footerHidden = NO;
        //停止下拉刷新
        [self.tableView headerEndRefreshing];
    } failure:^(NSError *error) {
        WKLog(@"%@",error);
    }];

}
/**
 *  加载更多数据
 */
- (void)loadMoreData
{
    WKOnlyTextParam *param = [[WKOnlyTextParam alloc] init];
    [WKOnlyTextHttpTool getThrougNewDataListWithParam:param success:^(WKOnlyTextResult *result) {
        //如果返回数据为空则返回;
        if (result == nil) return;
//        //[self.statuses addObjectsFromArray:result.homeDatas];
//        NSRange range = NSMakeRange(0, result.homeDatas.count);
//        NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
//        [self.statuses insertObjects:result.homeDatas atIndexes:set];
        
        [self.statuses addObjectsFromArray:result.homeDatas];

//        self.statuses = result.homeDatas;
        //刷新数据
        [self.tableView reloadData];
        //停止下拉刷新
        [self.tableView footerEndRefreshing];
    } failure:^(NSError *error) {
        //停止下拉刷新
        [self.tableView footerEndRefreshing];
        WKLog(@"%@",error);
    }];

}

- (WKTableViewCellFrame *)tablecell
{
    if(_tablecell == nil)
    {
        WKTableViewCellFrame *tablecell = [[WKTableViewCellFrame alloc]init];
        _tablecell = tablecell;
    }
    return _tablecell;
}

- (void)loadNewdata
{
    WKOnlyTextParam *param = [[WKOnlyTextParam alloc] init];
    [WKOnlyTextHttpTool getThrougNewDataListWithParam:param success:^(WKOnlyTextResult *result) {
        
        [self.statuses addObjectsFromArray:result.homeDatas];

        //显示底部刷新控件
        self.tableView.footerHidden = NO;
        //刷新数据
        [self.tableView reloadData];
        
        //停止下拉刷新
        [self.tableView headerEndRefreshing];
    } failure:^(NSError *error) {
        //停止下拉刷新
        [self.tableView headerEndRefreshing];
        WKLog(@"%@",error);
    }];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.statuses.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WKTableViewCell *cell = [WKTableViewCell cellWithTableView:tableView];
    self.tablecell.status = self.statuses[indexPath.row];
    cell.tableCellFrame = self.tablecell;
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WKTableViewCellFrame *cell = [[WKTableViewCellFrame alloc]init];
    cell.status = self.statuses[indexPath.row];
    return cell.cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //去掉cell的点击
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //跳转控制器
    WKCellPuthinfoTableView *cellinfo = [[WKCellPuthinfoTableView alloc]init];
    
    cellinfo.cellFreme.status = self.statuses[indexPath.row];
    cellinfo.cellFreme = self.tablecell;
    
    [self.navigation pushViewController:cellinfo animated:YES];
    
    NSLog(@"点击");
}


@end
